﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FIRADriverLibrary;
using RobotRace.engine.agents;
using RobotRace.engine.agents.implementation;
using RobotRace.Geometry;
using RobotRaceGUI.AgentConfigurationForms;

namespace RobotRaceGUI
{
    public partial class NewTeamForm : Form
    {
        public Agent NewAgent { get; private set; }
        public Form AgentConfigurationForm { get; private set; }
        public Boolean CreateButtonClicked { get; private set; }

        private List<FIRARobotState> robots;
        private List<Point2D> checkpoints;

        public NewTeamForm(List<FIRARobotState> unassignedRobotsList, List<Point2D> checkpoints)
        {
            this.checkpoints = checkpoints;
            InitializeComponent();
            unassignedRobots.DataSource = unassignedRobotsList;
            availableAgents.Items.Add("Simple point follower");
            availableAgents.Items.Add("PID point follower");
            availableAgents.Items.Add("PID path follower");
        }


        private void configureAgentButton_Click(object sender, EventArgs e)
        {
            String selectedValue = availableAgents.SelectedItem as String;
            if (selectedValue == null)
            {
                return;
            }
            FIRARobotState robot = unassignedRobots.SelectedItem as FIRARobotState;
            if (robot == null)
            {
                return;
            }
            float x = (float)robot.x;
            float y = (float)robot.y;
            Point2D robotStartingPoint = new Point2D(x, y);
            if (selectedValue == "Simple point follower")
            {
                SimplePointFollower agent = new SimplePointFollower(robotStartingPoint, robot, checkpoints);
                NewAgent = agent;
                NewAgent.Name = "Simple: " + robot.ToString();
                var form = new SimplePathFollowerConfigurationForm();
                form.agent = agent;
                form.Show();
                AgentConfigurationForm = form;
            }
            else if (selectedValue == "PID point follower")
            {
                var agent = new PIDPointFollower(robotStartingPoint, robot, checkpoints);
                NewAgent = agent;
                NewAgent.Name = "PID (point): " + robot.ToString();
                var form = new PIDPointFollowerConfigurationForm();
                form.agent = agent;
                form.Show();
                AgentConfigurationForm = form;
            }
            else if (selectedValue == "PID path follower")
            {
                var agent = new PIDPathFollower(robotStartingPoint, robot, checkpoints);
                NewAgent = agent;
                NewAgent.Name = "PID (Path): " + robot.ToString();
                var form = new PIDPathFollowerConfigurationForm();
                form.agent = agent;
                form.Show();
                AgentConfigurationForm = form;
            }
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            this.CreateButtonClicked = true;
            Hide();
        }

        public new void Show()
        {
            this.CreateButtonClicked = false;
            base.Show();
        }
    }

    public class AgentConfigurationForm : Form
    {
        public Agent Agent { get; private set; }
    }
}
